<template>
	<view>
		<view>wifi:{{apSsid}}</view>
		<input type="text" v-model="apPassword" placeholder="请输入wifi密码" />
		<view>提示消息:{{notice}}</view>
		<view>ip:{{ipAddress}}</view>
		<button @click="connectToWifi">点击配网</button>
	</view>
</template>

<script>
	import ByteUtil from "../../utils/wifi/byteUtil.js"
	import CRC8 from "../../utils/wifi/crc8.js"
	import DatumCode from "../../utils/wifi/datumCode.js"
	import util from "../../utils/util.js"

	export default {
		data() {
			return {
				apSsid: '',
				apBssid: '',
				apPassword: '',
				ipAddress: '',
				frequency: '',
				notice: '', //页面提示消息
				
				udp:null,
				serverUdp:null,
				listenPort:18266,
				sendPort:'',
				//send 7001
				messageForSend:[],
				connected:false,
				ready:false,
			}
		},
		onLoad() {
			this.getIpAddress()
			this.getWifiInfo();			
			//0xe9,0x128,0x123,0x118,0x129,0x6e,0xac,0x12a,0xfe,0xe2,0x12b,0x111,0xc7,0x12c,0x8f,0xb6,0x143,0x84,0xb4,0x12d,0xd8,0x72,0x12e,0x80,0x68,0x12f,0x99,0x8e,0x144,0x68,0x8e,0x130,0x9d,0x9b,0x131,0x79,0xeb,0x132,0x4a,0xaf,0x145,0x5b,0x7b,0x133,0xab,0xdb,0x134,0x7c,0x4b,0x135,0x11d,0x11e,0x146,0xe3,0xbb,0x136,0xae,0x2d,0x137,0xfc,0x10d,0x138,0xc8,0x127,0x147,0xa1,0xaa,0x139,0xe5,0x11c,0x13a,0xa4,0x7c,0x13b,0x91,0x68,0x148,0xff,0xdc,0x13c,0xe6,0x3c,0x13d,0xd3,0x4d,0x13e,0x127,0x4c,0x13f,0x11e,0xeb,0x140,0x31,0x4b,0x141,0xe8,0xcb,0x142,0x2f
		},
		onShow(){
			if(this.serverUdp){
				this.serverUdp.close();
			}
			this.serverUdp = wx.createUDPSocket();						
			let port = this.serverUdp.bind(18266)
			console.log(port,'链接成功了吗');
			//最好的方式是不通过这个监听端口来判断，而是后台收到设备的信号，然后给的反馈信息
			if(port != this.listenPort){
				this.notice = '初始化失败,请稍后再试';
			}else{
				this.ready = true;
			}
			
			this.serverUdp.onMessage(res=>{				
				console.log('啥情况',res);
				let unit8Arr = new Uint8Array(res.message);
				//收到消息就是网络链接成功	
				this.notice = '网络连接成功'
				this.connected = true;
				wx.hideLoading({});
				// let encodedString = String.fromCharCode.apply(null, unit8Arr);
				// let data = decodeURIComponent(escape((encodedString)));					
				console.log(unit8Arr.length);
			})
			this.serverUdp.onError(res=>{
				console.log('监听失败把',res);
			})
		},
		onHide() {
			this.serverUdp.close();
		},
		methods: {
			getWifiInfo() {
				let that = this;
				uni.authorize({
					scope: 'scope.userLocation',
					success() {
						uni.getLocation({
							type: 'wgs84',
							success: function(res) {
								uni.startWifi({
									success(res) {
										uni.getConnectedWifi({
											success(res) {
												if ('getConnectedWifi:ok' === res
													.errMsg) {
													that.apSsid = res.wifi.SSID
													that.apBssid = res.wifi.BSSID;
													that.frequency = res.wifi
													.frequency;
													if (res.wifi.frequency == 5200) {
														that.notice = '暂不支持5G Wifi';
													}
												} else {
													that.notice = "请连接路由器"
												}
											},
											fail(res) {
												that.notice = '请打开GPS定位';
											}
										})
									},
									fail: function(res) {
										that.notice = '请连接路由器!'
									}
								})
							},
							fail(r) {
								that.notice = '请打开GPS定位'
							}
						});
					},
					fail() {

					}
				});
			},
			getIpAddress() {
				let that = this;
				uni.getLocalIPAddress({
					success(res) {
						that.ipAddress = res.localip;
					},
					fail(r) {
						console.log(r, '获取ip失败')
					}
				})
			},
			connectToWifi() {
				if(!this.ready){
					util.message("请稍后再试...");
					return;
				}
				//apSsid, apBssid, apPassword, ipAddress						
				if (!this.apSsid || !this.apBssid || !this.apPassword || !this.ipAddress) {
					util.warning('信息不全');
					return;
				}
				
				wx.showLoading({
					title:'连接中...'
				})
				let apSsid = ByteUtil.toUint8Arr(this.apSsid);
				let apBssid = ByteUtil.parseBssid2bytes(this.apBssid);
				let apPassword = ByteUtil.toUint8Arr(this.apPassword);
				let ipAddress = ByteUtil.ipAddressToByte(this.ipAddress);

				let dc = new DatumCode(apSsid, apBssid, apPassword, ipAddress);
				this.messageForSend = dc.getU8s();
				
				
				
				if(this.udp){
					this.udp.close();
				}
				this.udp = wx.createUDPSocket();
				this.sendPort = this.udp.bind();
				console.log("端口",this.sendPort);								
				this.send()																
			},
			send(){				
				if(this.connected) return;
				let header = [515, 514, 513, 512]
				for (let j = 0; j < 10; j++) {
					for (let i = 0; i < header.length; i++) {						
						this.udp.send({
							address: '255.255.255.255',
							port: this.sendPort,
							message: '1'.repeat(header[i])
						});
					}
				}
				for (let i = 0; i < this.messageForSend.length; i++) {
					this.udp.send({
						address: '255.255.255.255',
						port: this.sendPort, //其实这个影响不大，可以随便写
						message: '1'.repeat(this.messageForSend[i])
					});
				}
				setTimeout(()=>{
					this.send();
				},1000);				
			}
		}
	}
</script>

<style>

</style>
